package com.springboot.util;

import com.springboot.bean.Code;
import com.springboot.biz.ItemBiz;
import com.springboot.biz.PipeBiz;
import com.springboot.biz.ProjectBiz;
import com.springboot.entity.Item;
import com.springboot.entity.Pipe;
import com.springboot.entity.Project;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.Iterator;
import java.util.List;

/**
 * 项目规范转换工具类
 */
@Component
@Transactional
public class TranHelper {

    @Resource
    private ProjectBiz projectBiz;
    @Resource
    private PipeBiz pipeBiz;
    @Resource
    private ItemBiz itemBiz;

    private String codes = "CPF, GYF, ICF, LHF, BRF, MHF, OSF, OCF, OFF, REF, SKF";
    private DecimalFormat foramt = new DecimalFormat("#000");

    public void trans(Project project) {
        int photo = 1;

        List<Pipe> pipes = pipeBiz.findListPipe(project);
        for (int i = 0; pipes != null && i < pipes.size(); i++) {
            Pipe pipe = pipes.get(i);
            List<Item> items = itemBiz.findListItem(pipe);
            Iterator<Item> iterator = items.iterator();
            while (iterator.hasNext()) {
                Item item = iterator.next();
                if ("ST".equals(item.getCode())) {
                    iterator.remove();
                    continue;
                }
                if ("WL".equals(item.getCode()))
                    item.setCode("WLC");
                if (item.getCode().endsWith("J") || codes.contains(item.getCode()))
                    item.setCode(item.getCode().substring(0, item.getCode().length() - 1));
                Code code = CodeHelper.findInfo(item.getCode(), project.getStandard());
                if ("-".equals(code.getCode2()))
                    iterator.remove();
                item.setCode(code.getCode2());
                item.setPhoto("");
                if (!StringUtils.isEmpty(item.getPicture()))
                    item.setPhoto(foramt.format(photo++));
            }
            pipe.setItems(items);
        }

        project.setStandard("MSCC 2004");
        projectBiz.insertProject(project);
        for (int i = 0; pipes != null && i < pipes.size(); i++) {
            pipes.get(i).setProject(project);
            pipeBiz.insertPipe(pipes.get(i));
            List<Item> items = pipes.get(i).getItems();
            for (int j = 0; items != null && j < items.size(); j++) {
                items.get(j).setPipe(pipes.get(i));
                itemBiz.insertItem(items.get(j));
            }
        }
    }


    public void convert(Project project, String standard) {
        List<Pipe> pipes = pipeBiz.findListPipe(project);
        for (int i = 0; pipes != null && i < pipes.size(); i++)
            changePipe(project, pipes.get(i), standard, 1);

        project.setStandard(standard);
        projectBiz.insertProject(project);

        pipes.stream().forEach(pipe -> {
            pipe.setProject(project);
            pipeBiz.insertPipe(pipe);
            pipe.getItems().stream().forEach(item -> {
                item.setPipe(pipe);
                itemBiz.insertItem(item);
            });
        });
    }
    private Pipe changePipe(Project project, Pipe pipe, String standard, int photo) {
        List<Item> items = itemBiz.findListItem(pipe);
        for (int i = 0; i < items.size(); i++) {
            if (changeItem(project, items.get(i), standard, photo) == null)
                items.remove(i);
        }
        pipe.setItems(items);
        return pipe;
    }

    private Item changeItem(Project project, Item item, String standard, int photo) {
        String code = CodeHelper.convert(item.getCode(), project.getStandard(), standard);
        if (StringUtils.isEmpty(code))
            return null;
        item.setCode(code);
        if (!StringUtils.isEmpty(item.getPicture()))
            item.setPhoto(foramt.format(photo++));
        else
            item.setPhoto("");
        return item;
    }
}
